<template>
  <div class="workurging">
    <div class="plantime" v-if='showlist'>
      <div class="singlechoose">
        <el-radio v-model="radio" label="1">成员机构→银联</el-radio>
        <el-radio v-model="radio" label="2">银联→成员机构</el-radio>
      </div>
      <div class="formbox">
        <div class="left">
          <el-form label-width="auto" size="mini" >
            <el-row>
              <el-col :span="7" style="margin-right:80px">
                <el-form-item label="服务级别：">
                  <el-select
                    v-model="listQuery.type"
                    :placeholder="$t('table.type')"
                    clearable
                    class="filter-item"
                    style="width: 150px"
                  >
                    <el-option
                      v-for="item in calendarTypeOptions"
                      :key="item.key"
                      :label="item.display_name + '(' + item.key + ')'"
                      :value="item.key"
                    />
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="7">
                <el-form-item label="运营维护时间段：">
                  <el-date-picker
                    style="width: 330px"
                    v-model="search_data"
                    type="datetimerange"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    :default-time="['12:00:00']"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div class="right">
          <el-button
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >
            {{ $t("table.search") }}
          </el-button>
        </div>
      </div>
    </div>
    
    <!-- ---- -->
    <div class="contentT" v-if='showlist'>
      <div class="tips">近1个月华夏银行发起的交易协查信息</div>
      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        :header-cell-style="{
          background: '#EAF1FF',
          'text-align': 'center',
          color: '#000',
        }"
          :cell-style="{ 'text-align': 'center' }"
        fit
        highlight-current-row
        style="width: 100%"
        @sort-change="sortChange"
        :border="false"
      >
        <el-table-column
          :label="$t('table.id')"
          prop="id"
          sortable="custom"
          align="center"
          width="80"
          :class-name="getSortClass('id')"
        >
          <template slot-scope="{ row }">
            <span>{{ row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="银联单号" width="240" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.timestamp | parseTime("{y}-{m}-{d} {h}:{i}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="交易日期时间" min-width="240">
          <template slot-scope="{ row }">
            <span class="link-type" @click="handleUpdate(row)">{{
              row.title
            }}</span>
            <el-tag>{{ row.type | typeFilter }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="期望完成处理时间"
          width="240"
          align="center"
        >
          <template slot-scope="{ row }">
            <span>{{ row.author }}</span>
          </template>
        </el-table-column>
        <el-table-column
        
          label="预计完成处理时间"
          width="240"
          align="center"
        >
          <template slot-scope="{ row }">
            <span style="color: red">{{ row.reviewer }}</span>
          </template>
        </el-table-column>
  
      
        <el-table-column
          label="业务状态"
          class-name="status-col"
          width="112"
        >
          <template slot-scope="{ row }">
            <el-tag :type="row.status | statusFilter">
              {{ row.status }}
            </el-tag>
          </template>
        </el-table-column>
          <el-table-column
          label="上报/接收状态"
          align="center"
          width="222"
        >
          <template slot-scope="{ row }">
            <span
              v-if="row.pageviews"
              class="link-type"
              @click="handleFetchPv(row.pageviews)"
              >{{ row.pageviews }}</span
            >
            <span v-else>0</span>
          </template>
        </el-table-column>
              <el-table-column label="描述信息" width="166">
          <template slot-scope="{ row }">
            <svg-icon
              v-for="n in +row.importance"
              :key="n"
              icon-class="star"
              class="meta-item__icon"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="详情"
          align="center"
          width="106"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="{ row, $index }">
            <el-button type="primary" size="mini" @click="handleUpdate(row)">
              {{ $t("table.edit") }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />


    </div>


  

      <transition name="fade-transform" mode="out-in">
     <WorkUrgingDetail v-if="showdetail"/>
     </transition>
  </div>
</template>

<script>
import WorkUrgingDetail  from './WorkUrgingDetail'
import {
  fetchList,
  fetchPv,
  createArticle,
  updateArticle,
} from "@/api/article";
import waves from "@/directive/waves"; // waves directive
import { parseTime } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination

const calendarTypeOptions = [
  { key: "CN", display_name: "China" },
  { key: "US", display_name: "USA" },
  { key: "JP", display_name: "Japan" },
  { key: "EU", display_name: "Eurozone" },
];

// arr to obj, such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name;
  return acc;
}, {});

export default {
  name: "ComplexTable",
  components: { Pagination ,WorkUrgingDetail},
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger",
      };
      return statusMap[status];
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type];
    },
  },
  data() {
    return {
     showlist:true,
      showdetail:false,
      radio: "1",
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      value1: "",
      search_data: {
        startTime: "",
        endTime: "",
      },
      listQuery: {
        page: 1,
        limit: 20,
        // importance: undefined,
        // title: undefined,
        type: undefined,
        sort: "+id",
      },
      importanceOptions: [1, 2, 3],
      calendarTypeOptions,
      sortOptions: [
        { label: "ID Ascending", key: "+id" },
        { label: "ID Descending", key: "-id" },
      ],
      statusOptions: ["published", "draft", "deleted"],
      showReviewer: false,
      temp: {
        id: undefined,
        importance: 1,
        remark: "",
        timestamp: new Date(),
        title: "",
        type: "",
        status: "published",
      },
    
      textMap: {
        update: "Edit",
        create: "Create",
      },
      // dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [
          { required: true, message: "type is required", trigger: "change" },
        ],
        timestamp: [
          {
            type: "date",
            required: true,
            message: "timestamp is required",
            trigger: "change",
          },
        ],
        title: [
          { required: true, message: "title is required", trigger: "blur" },
        ],
      },
      downloadLoading: false,
    };
  },
  created() {
    this.getList();
  },
        mounted(){
    this.$eventBus.$on("zipao",()=>{
        this.showlist=true,
        this.showdetail=false
    })
  },
  methods: {
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.list = response.data.items;
        this.total = response.data.total;

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false;
        }, 1.5 * 1000);
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: "操作成功",
        type: "success",
      });
      row.status = status;
    },
    sortChange(data) {
      const { prop, order } = data;
      if (prop === "id") {
        this.sortByID(order);
      }
    },
    // sortByID(order) {
    //   if (order === "ascending") {
    //     this.listQuery.sort = "+id";
    //   } else {
    //     this.listQuery.sort = "-id";
    //   }
    //   this.handleFilter();
    // },
    // 返回隐藏
    
      
     

    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: "",
        timestamp: new Date(),
        title: "",
        status: "published",
        type: "",
      };
    },
    // handleCreate() {
    //   this.resetTemp()
    //   this.dialogStatus = 'create'
    //   this.dialogFormVisible = true
    //   this.$nextTick(() => {
    //     this.$refs['dataForm'].clearValidate()
    //   })
    // },
    // createData() {
    //   this.$refs["dataForm"].validate((valid) => {
    //     if (valid) {
    //       this.temp.id = parseInt(Math.random() * 100) + 1024; // mock a id
    //       this.temp.author = "vue-element-admin";
    //       createArticle(this.temp).then(() => {
    //         this.list.unshift(this.temp);
    //         this.dialogFormVisible = false;
    //         this.$notify({
    //           title: "成功",
    //           message: "创建成功",
    //           type: "success",
    //           duration: 2000,
    //         });
    //       });
    //     }
    //   });
    // },
    handleUpdate(row) {
      // this.temp = Object.assign({}, row); // copy obj
      // this.temp.timestamp = new Date(this.temp.timestamp);
      // this.dialogStatus = "update";
      // this.dialogFormVisible = true;
      // this.$nextTick(() => {
      //   this.$refs["dataForm"].clearValidate();
      // });
      this.showlist=false
      this.showdetail=true
    },
    // updateData() {
    //   this.$refs["dataForm"].validate((valid) => {
    //     if (valid) {
    //       const tempData = Object.assign({}, this.temp);
    //       tempData.timestamp = +new Date(tempData.timestamp); // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
    //       updateArticle(tempData).then(() => {
    //         const index = this.list.findIndex((v) => v.id === this.temp.id);
    //         this.list.splice(index, 1, this.temp);
    //         this.dialogFormVisible = false;
    //         this.$notify({
    //           title: "成功",
    //           message: "更新成功",
    //           type: "success",
    //           duration: 2000,
    //         });
    //       });
    //     }
    //   });
    // },
    // handleDelete(row, index) {
    //   this.$notify({
    //     title: '成功',
    //     message: '删除成功',
    //     type: 'success',
    //     duration: 2000
    //   })
    //   this.list.splice(index, 1)
    // },
    // handleFetchPv(pv) {
    //   fetchPv(pv).then((response) => {
    //     this.pvData = response.data.pvData;
    //     this.dialogPvVisible = true;
    //   });
    // },
    // handleDownload() {
    //   this.downloadLoading = true
    //   import('@/vendor/Export2Excel').then(excel => {
    //     const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
    //     const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
    //     const data = this.formatJson(filterVal)
    //     excel.export_json_to_excel({
    //       header: tHeader,
    //       data,
    //       filename: 'table-list'
    //     })
    //     this.downloadLoading = false
    //   })
    // },
    formatJson(filterVal) {
      return this.list.map((v) =>
        filterVal.map((j) => {
          if (j === "timestamp") {
            return parseTime(v[j]);
          } else {
            return v[j];
          }
        })
      );
    },
    getSortClass: function (key) {
      const sort = this.listQuery.sort;
      return sort === `+${key}` ? "ascending" : "descending";
    },
  },
  mounted(){
       this.$eventBus.$on("zipao",()=>{
        this.showlist=true
        this.showdetail=false
       })
  }
};
</script>

<style lang='scss' scoped>
@import '@/styles/Fadeinout.scss';
.workurging {
  padding: 20px;
  .plantime {
    background-color: #fff;
    width: 100%;
    height: 100%;
    border-radius: 8px;

    // text-align: center;
    padding: 20px;
     padding-bottom: 0;
    font-size: 14px;
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
    margin-bottom: 20px;
    .formbox {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .left {
      margin-top: 22px;
      box-sizing: border-box;
      margin-right: 100px;
    }
  }
  .contentT {
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    background-color: #fff;
    height: 100%;
    width: 100%;
    padding: 20px;

    .tips {
      width: 100%;
      height: 22px;
      font-size: 16px;
      // margin-left: 20px;
      font-weight: 500;
      color: #5084fb;
      line-height: 22px;
      margin-bottom: 20px;
    }
  }
}
</style>